// import * as THREE from 'three'
// import {RoomEnvironment} from "three/examples/jsm/environments/RoomEnvironment.js";
// import {EXRLoader} from "three/examples/jsm/loaders/EXRLoader.js";
// /*
// * 添加灯光 加载环境贴图
// * */
// export  const addLight = (scene,renderer) => {
//   const pmremGenerator = new THREE.PMREMGenerator(renderer); // 创建 PMREMGenerator 对象
//   pmremGenerator.compileEquirectangularShader();
//   // 调整环境纹理的强度
//   const environment = new RoomEnvironment();
//   scene.environment = pmremGenerator.fromScene(environment, 0.5).texture; // 将强度从 1 调整为 0.5
//   // 调整环境光的强度
//   const ambientLight = new THREE.AmbientLight(0xababab, 0.01); // 将强度从 0.004 调整为 0.002
//   scene.add(ambientLight);
// };
// export const loadEnv = (scene,renderer) => {
//   new EXRLoader().load(`exr/3.exr`, function (texture) {
//     const pmremGenerator = new THREE.PMREMGenerator(renderer);
//     pmremGenerator.compileEquirectangularShader();
//     // 生成预过滤的环境贴图
//     const envTexture = pmremGenerator.fromEquirectangular(texture).texture;
//     scene.background = envTexture;
//   });
// }
// export const renderControls = (controls)=>{
//   controls.update();
// }
//
import * as THREE from 'three'
import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js'
import rolling_hills_2k from '@/assets/hdr/rolling_hills_2k.hdr'
import gsap from 'gsap'
/*
 * 添加灯光 加载环境贴图
 * */
export const addLight = (scene, renderer) => {
  const ambientLight = new THREE.AmbientLight('#FFFFFF', 1)
  // 平行光源
  const directionalLight1 = new THREE.DirectionalLight('#ffffff', 1)
  const directionalLight2 = new THREE.DirectionalLight('#ffffff', 1)
  const directionalLight3 = new THREE.DirectionalLight('#ffffff', 1)
  const directionalLight4 = new THREE.DirectionalLight('#ffffff', 1)
  directionalLight1.position.set(70, 50, 0)
  directionalLight2.position.set(-70, 50, 0)
  directionalLight3.position.set(0, 50, 30)
  directionalLight4.position.set(0, 50, -30)
  scene.add(directionalLight1)
  scene.add(directionalLight2)
  scene.add(directionalLight3)
  scene.add(directionalLight4)
  scene.add(ambientLight)
}
export const loadEnv = (scene, renderer) => {
  const rgbeLoader = new RGBELoader()
  rgbeLoader
    .loadAsync(rolling_hills_2k)
    .then((texture) => {
      texture.mapping = THREE.EquirectangularReflectionMapping
      scene.background = texture
      scene.environment = texture
    })
    .catch((error) => {
      console.error('Failed to load environment map:', error)
    })
}
/*
 * 修改renderer的颜色透明度
 * */
export const setRendererOpacity = ({
                                     renderer,
                                     color = 0xff0000,
                                     startAlpha = 1,
                                     endAlpha = 0,
                                     duration = 0.5,
                                     onComplete
                                   }) => {
  return new Promise((resolve) => {
    renderer.setClearColor(color, 1); // 初始设置为完全不透明

    // 创建一个对象来存储透明度值
    const alphaObject = { alpha: startAlpha };

    // 使用 GSAP 动画化透明度值
    gsap.to(alphaObject, {
      duration: duration, // 动画持续时间（秒）
      ease: 'power1.inOut', // 缓动函数
      alpha: endAlpha, // 目标透明度
      onUpdate: () => {
        // 在动画的每一帧中，动态更新背景颜色的透明度
        renderer.setClearColor(color, alphaObject.alpha); // 更新背景颜色透明度
      },
      onComplete: () => {
        // 调用外部传入的 onComplete 回调（如果有）
        onComplete && onComplete();
        // 解决 Promise
        resolve();
      }
    });
  });
};
